﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Stanza.MSBuild.Tasks
{
    public class TempStream : Stream
    {
        private MemoryStream memStream = new MemoryStream();
        private int closeCounter = 2;

        public TempStream()
        {
        }

        public override bool CanRead
        {
            get { return memStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return memStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return memStream.CanWrite; }
        }

        public override void Flush()
        {
            memStream.Flush();
        }

        public override long Length
        {
            get { return memStream.Length; }
        }

        public override long Position
        {
            get { return memStream.Position; }
            set { memStream.Position = value; }
        }

        public override int Read( byte[] buffer, int offset, int count )
        {
            return memStream.Read( buffer, offset, count );
        }

        public override long Seek( long offset, SeekOrigin origin )
        {
            return memStream.Seek( offset, origin );
        }

        public override void SetLength( long value )
        {
            memStream.SetLength( value );
        }

        public override void Write( byte[] buffer, int offset, int count )
        {
            memStream.Write( buffer, offset, count );
        }

        public override void Close()
        {
            if( --closeCounter == 0 )
                base.Close();
            else
                memStream.Position = 0;
        }
    }
}
